﻿using Gobang.ToolClass.Area;

namespace Gobang.Client.Common
{
    internal class AreaCache
    {
        private static AreaNode[] _areaNodes;
        private static TreeNode[] _treeNodes;
        public static AreaNode[] AreaNodes
        {
            get => _areaNodes;
            set
            {
                if (_areaNodes == null)
                {
                    _areaNodes = value;
                    _treeNodes = new TreeNode[_areaNodes.Length];
                    int index = 0;
                    foreach (AreaNode node in _areaNodes)
                    {
                        var arr = node.AreaNodes.Select(t =>
                        {
                            var tem = new TreeNode(t.AreaName);
                            tem.Tag = t;
                            tem.EndEdit(false);
                            return tem;
                        }).ToArray();

                        var tnode = new TreeNode(node.AreaName, arr);
                        tnode.Expand();
                        tnode.EndEdit(false);
                        tnode.Tag = node;
                        _treeNodes[index++] = tnode;
                    }
                }
            }
        }
        public static TreeNode[] TreeNodes
        {
            get => _treeNodes;
        }
        public static List<Room> FindDesks(string roomId)
        {
            List<Room> result = null;
            foreach (var item in _areaNodes)
            {
                foreach (var item2 in item.AreaNodes)
                {
                    if (item2.AreaId == roomId)
                    {
                        result = item2.Rooms;
                        goto End;
                    }
                }
            }
        End:
            return result;
        }
    }
}
